<?php
namespace App\Http\Controllers\Admin\Point;

use App\Service\Admin\Point\GcateService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class GcateController extends BaseController
{
    //商品

    public function __construct(GcateService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        if (!$res) {
            return $this->toJson(-2, 'no data');
        }
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        $res = $this->db->getList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function modify(Request $request)
    {
        $id = $request->post('id', '');
        $pas['title'] = $request->post('title', '');
        $pas['pid'] = $request->post('pid', 0);
        $pas['img'] = $request->post('img', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '商品名称必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->addOne($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}